//
//  SJTableViewController.m
//  DFront
//
//  Created by lanou on 15/10/27.
//  Copyright © 2015年 xuzenghui. All rights reserved.
//

#import "SJTableViewController.h"
#import "SJTableViewCell.h"
#import "PKRequestManager.h"
#import "UIImageView+WebCache.h"
#import "DFwebViewController.h"
#import "SJEverySelectModel.h"
#import "CommonTableViewCell.h"
#import "MJRefresh.h"

@interface SJTableViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray *dataSourceArray;
@property (nonatomic, strong) NSMutableArray *imageArray;

@property (nonatomic, assign) int page;
@end

@implementation SJTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _dataSourceArray = [NSMutableArray array];
    _imageArray = [NSMutableArray array];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[CommonTableViewCell class] forCellReuseIdentifier:@"acell"];
    [self.tableView registerClass:[SJTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self requestDataWithType:0];
    
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self requestDataWithType:0];
    }];
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self requestDataWithType:1];
    }];
}

- (void)requestDataWithType:(NSInteger)type
{
    if (type == 0) {
        _page = 1;
    }
    else
    {
        _page ++;
    }
    NSString *str1 = [NSString stringWithFormat:@"http://lib.wap.zol.com.cn/bbs/ios/getJingxuan.php?bbs=sjbbs&page=%d&vs=440",_page];
    [PKRequestManager requestWithType:GET urlString:str1 parDic:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        if (type == 0) {
            [self.dataSourceArray removeAllObjects];
        }
        NSArray *listArray = dic[@"list"];
        for (NSDictionary *moDic in listArray) {
            SJEverySelectModel *model = [[SJEverySelectModel alloc] init];
            [model setValuesForKeysWithDictionary:moDic];
            [_dataSourceArray addObject:model];
            
        }
        
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
    } error:^(NSError *error) {
        NSLog(@"页面请求失败");
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SJEverySelectModel *model = _dataSourceArray[indexPath.row];
    if (model.picArray.count != 0) {
        return 170;
    }
    else
    {
        return 150;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return _dataSourceArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SJEverySelectModel *model = _dataSourceArray[indexPath.row];
    if (model.picArray.count != 0) {
    SJTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.titleLabel.text = model.title;
    [cell.authorImg sd_setImageWithURL:[NSURL URLWithString:model.photo]];
    cell.authorNameLabel.text = model.nickname;
    NSString *str = [NSString stringWithFormat:@"%@",model.reply];
    cell.replyLabel.text = [str stringByAppendingString:@" 回复"];
    NSString *str1 = model.date;
    cell.replyTimeLabel.text = [str1 substringFromIndex:5];
    
    if (model.picArray.count == 1) {
        [cell.aImage sd_setImageWithURL:[NSURL URLWithString:model.picArray[0]]];
        cell.bImage.image = [UIImage imageNamed:@"white"];
        cell.cImage.image = [UIImage imageNamed:@"white"];
    }
    else if(model.picArray.count == 2){
        [cell.aImage sd_setImageWithURL:[NSURL URLWithString:model.picArray[0]]];
        [cell.bImage sd_setImageWithURL:[NSURL URLWithString:model.picArray[1]]];
        cell.cImage.image = [UIImage imageNamed:@"white"];
    }
    else if(model.picArray.count == 3){
        [cell.aImage sd_setImageWithURL:[NSURL URLWithString:model.picArray[0]]];
        [cell.bImage sd_setImageWithURL:[NSURL URLWithString:model.picArray[1]]];
        [cell.cImage sd_setImageWithURL:[NSURL URLWithString:model.picArray[2]]];
    }

    return cell;
    }
    else
    {
        CommonTableViewCell *acell = [tableView dequeueReusableCellWithIdentifier:@"acell"];
        acell.titleLabel.text = model.title;
        [acell.authorImg sd_setImageWithURL:[NSURL URLWithString:model.photo]];
        acell.authorNameLabel.text = model.nickname;
        NSString *str = [NSString stringWithFormat:@"%@",model.reply];
        acell.replyLabel.text = [str stringByAppendingString:@" 回复"];
        NSString *str1 = model.date;
        acell.replyTimeLabel.text = [str1 substringFromIndex:5];
        return acell;

    }
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DFwebViewController *webVC = [[DFwebViewController alloc] init];
    SJEverySelectModel *model = _dataSourceArray[indexPath.row];
    webVC.astr = model.bbs;
    webVC.bstr = model.boardid;
    webVC.cstr = model.bookid;
    [self.navigationController pushViewController:webVC animated:YES];

}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
